package com.ovopark.firecar.utils

import android.content.Context
import android.os.Build
import com.ovopark.firecar.common.Constants
import com.ovopark.firecar.common.Constants.LANGUAGE
import java.util.*

/**
 * 语言检测
 * Created by cz on 2016/11/21.
 */
object LanguageUtils {
    @JvmStatic
    fun getRegisterLanguage(context: Context?): String {
        val language = context?.let {
            SharedPreferencesUtils.getInstance(Constants.Prefs.PREFRENCE_NAME)
                ?.getParam(it, Constants.Prefs.LANGUAGE, LANGUAGE.DEFAULT)
        } as String
        return when (language) {
            LANGUAGE.SIMPLIFIED_CHINESE -> "SIMPLIFIED_CHINESE"
            LANGUAGE.ENGLISH -> "English"
            LANGUAGE.TRADITIONAL_CHINESE -> "TRADITIONAL_CHINESE"
            LANGUAGE.TAIWAN -> "TAIWAN"
            LANGUAGE.INDONESIA -> "INDONESIA"
            else -> "SIMPLIFIED_CHINESE"
        }
    }

    @JvmStatic
    fun getDisplayLanguage(context: Context): String {
        val locale: Locale
        locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            context.resources.configuration.locales[0]
        } else {
            context.resources.configuration.locale
        }
        val language = locale.language + "-" + locale.country
        return if (language.equals("zh", ignoreCase = true)) {
            LANGUAGE.SIMPLIFIED_CHINESE
        } else if (language.equals("en", ignoreCase = true)) {
            LANGUAGE.ENGLISH
        } else if (language.equals("zh-tw", ignoreCase = true)) {
            LANGUAGE.TAIWAN
        } else {
            LANGUAGE.SIMPLIFIED_CHINESE
        }
    }

    @JvmStatic
    fun getLanguageCode(language: String?): Locale {
        return when (language) {
            LANGUAGE.SIMPLIFIED_CHINESE -> Locale.SIMPLIFIED_CHINESE
            LANGUAGE.ENGLISH -> Locale.ENGLISH
            LANGUAGE.TAIWAN -> Locale.TAIWAN
            LANGUAGE.INDONESIA -> Locale("in")
            else -> Locale.SIMPLIFIED_CHINESE
        }
    }

    @JvmStatic
    fun getLanguage(context: Context?): String {
        val language = context?.let {
            SharedPreferencesUtils.getInstance(Constants.Prefs.PREFRENCE_NAME)
                ?.getParam(it, Constants.Prefs.LANGUAGE, LANGUAGE.DEFAULT)
        } as String
        return when (language) {
            LANGUAGE.SIMPLIFIED_CHINESE -> "cn"
            LANGUAGE.ENGLISH -> "en"
            LANGUAGE.TAIWAN -> "tw"
            LANGUAGE.INDONESIA -> "id"
            else -> "cn"
        }
    }
}